package com.avicit.zipkinkafkaesserver.controller;

import com.avicit.zipkinkafkaesserver.entity.ZipkinInterfaceCount;
import com.avicit.zipkinkafkaesserver.service.ZipkinInterfaceCountService;
import com.avicit.zipkinkafkaesserver.vo.InterfaceCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class ZipkinInterfaceCountController {

    @Autowired
    private ZipkinInterfaceCountService zipkinInterfaceCountService;

//    @GetMapping("/api/interfaceCounts")
//    public List<ZipkinInterfaceCount> interfaceCounts() {
//       return zipkinInterfaceCountService
//    }
    @GetMapping("/api/interfaceCounts")
    public String interfaceCounts(@RequestParam(required = false, defaultValue = "0") long startTime,
                                               @RequestParam(required = false, defaultValue = "0") long endTime,
                                               Model model
    ) {
        Map<String, Double> stringDoubleMap = zipkinInterfaceCountService.interfaceCounts(startTime, endTime);
        List<InterfaceCountVo> interfaceCountVos = new ArrayList<>();
        stringDoubleMap.entrySet().forEach(kv->{
            String key = kv.getKey();
            double value = kv.getValue();
            String[] split = key.split("@");
            interfaceCountVos.add(InterfaceCountVo
                    .builder()
                    .ip(split[0])
                    .serverName(split[1])
                    .interfaceName(split[3])
                    .status(split[4])
                    .count((int)value)
                    .build());
        });
        model.addAttribute("countVos",interfaceCountVos);
        return "apiInterfaceCount";
    }
    @GetMapping("/api/interfaceCounts/v2")
    public String interfaceCountsv2(@RequestParam(required = false) String queryDate,
                                               Model model
    ) {
        List<ZipkinInterfaceCount> interfaceCountList = zipkinInterfaceCountService.interfaceCountsv2(queryDate);
        model.addAttribute("countVos",interfaceCountList);
        return "apiInterfaceCountv2";
    }


}
